<?php

namespace Havoo\Core;

class Db
{
    protected $_dbHandler;
    protected $_result;

    public function connect($host, $user, $passwd, $dbname, $charset = 'utf8')
    {
        return $this->_dbHandler = PdoDriver::getInstance($host, $user, $passwd, $dbname, $charset);
    }

    public function selectAll($where = null, $orderBy = '', $start = null, $offset = null)
    {
        $where = $where ? ' where ' . $this->_formatWhere($where) : '';
        $sql = sprintf("select * from `%s` %s", $this->_table, $where);
        if ($orderBy) {
            $sql .= ' order by ' . $orderBy;
        }
        if (!is_null($start)) {
            $limit = 'limit ' . $start;
            if ($offset) {
                $limit .= ',' . $offset;
            } 
            $sql .= ' ' . $limit;
        }
        $pre = $this->_dbHandler->prepare($sql);
        $pre->execute();

        return $pre->fetchAll();
    }

    public function selectAllBySql($sql)
    {
        $pre = $this->_dbHandler->prepare($sql);
        $pre->execute();
        return $pre->fetchAll();
    }

    public function select($where = null)
    {
        $where = $where ? ' where ' . $this->_formatWhere($where) : '';
        $sql = sprintf("select * from `%s` %s", $this->_table, $where);
        $pre = $this->_dbHandler->prepare($sql);
        $p = $pre->execute();

        return $pre->fetch();
    }

    public function query($sql)
    {
        $pre = $this->_dbHandler->prepare($sql);
        $pre->execute();

        return $pre->rowCount();
    }

    public function delete($id)
    {
        $sql = sprintf("delete from `%s` where `id` = '%s'", $this->_table, $id);
        $pre->prepare($sql);
        $pre->execute();

        return $pre->rowCount();
    }

    public function add($data)
    {
        $sql = sprintf("insert into `%s` %s", $this->_table, $this->_formatInsert($data));

        return $this->query($sql);
    }

    public function update($where, $data)
    {
        $sql = sprintf("update `%s` set %s where 1 = 1 and %s", $this->_table, $this->_formatUpdate($data), $this->_formatWhere($where));

        return $this->query($sql);
    }

    private function _formatInsert($data)
    {
        $fields = array();
        $values = array();
        foreach ($data as $key => $value) {
            $fields[] = sprintf("`%s`", $key);
            $values[] = sprintf("'%s'", $value);
        }

        $field = implode(',', $fields);
        $value = implode(',', $values);

        return sprintf("(%s) values (%s)", $field, $value);
    }
    
    private function _formatWhere($data)
    {
        $where = array();
        foreach ($data as $key => $value) {
            $where[] = "{$key} = '{$value}'";
        }

        return implode(' AND ', $where);
    }

    private function _formatUpdate($data)
    {
        $where = array();
        foreach ($data as $key => $value) {
            $where[] = "{$key} = '{$value}'";
        }

        return implode(', ', $where);
    }

}
